package com.lbm.exercise;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mockStatic;

@RunWith(MockitoJUnitRunner.StrictStubs.class) // 修改为 StrictStubs 运行器
public class CalculationTest3 {

    @InjectMocks
    Calculation calculation;


    @Test
    public void test3(){
        System.out.println("你是指");
    }


    @Test
    public void test1() {
        try (MockedStatic<Division> div = mockStatic(Division.class)) { // 使用 try-with-resources
            div.when(() -> Division.divide(anyInt(), anyInt()))
                    .thenThrow(new RuntimeException("模拟异常")); // 使用具体异常类型

            boolean isError = false;
            try {
                calculation.compute(1, 2);
            } catch (Exception e) {
                isError = true;
                System.out.println("你好");
            } finally {
                assertTrue(isError);
            }
        }
    }

    @Test
    public void test2() {
        try (MockedStatic<Division> div = mockStatic(Division.class)) { // 使用 try-with-resources
            div.when(() -> Division.divide(anyInt(), anyInt()))
                    .thenThrow(new RuntimeException("模拟异常"));

            boolean isError = false;
            try {
                calculation.compute(1, 2);
            } catch (Exception e) {
                isError = true;
                System.out.println("你好");
            } finally {
                assertTrue(isError);
            }
        }
    }
}
